﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ScormModel
{
    [Serializable()]
    public class Organization
    {
        private string _type;
        private string _identifier = "";
        private string _author = "";
        private string _date = "";
        private string _description = "";
        private string _template = "";
        private string _title = "";
         [NonSerialized]
        private XmlNode _organizationContent;
        private List<Item> _listItem;
        private Sequencing _sequencing;
        [XmlIgnoreAttribute]
        public string Type
        {
            get { return this._type; }
            set { this._type = value; }
        }
        [XmlAttribute("identifier")]
        public string Identifier
        {
            get { return this._identifier; }
            set { this._identifier = value; }
        }
        [XmlAttribute("author")]
        public string Author
        {
            get { return this._author; }
            set { this._author = value; }
        }
        [XmlAttribute("date")]
        public string Date
        {
            get { return this._date; }
            set { this._date = value; }
        }
        [XmlAttribute("description")]
        public string Description
        {
            get { return this._description; }
            set { this._description = value; }
        }
        [XmlAttribute("Template")]
        public string Template
        {
            get { return this._template; }
            set { this._template = value; }
        }

        [XmlElement("title")]
        public string Title
        {
            get { return this._title; }
            set { this._title = value; }
        }
        [XmlElement("item")]
        public List<Item> ListItem
        {
            get { return this._listItem; }
            set { this._listItem = value; }
        }
        [XmlElement("sequencing", Namespace = "http://www.imsglobal.org/xsd/imsss")]
        public Sequencing Sequencing
        {
            get { return this._sequencing; }
            set { this._sequencing = value; }
        }
        
        [XmlIgnoreAttribute]
        public XmlNode OrganizationContent
        {
            get { return this._organizationContent; }
            set { this._organizationContent = value; }
        }
        public void buildOrganization(string courseTemplate)
        {
            XmlAttributeCollection attList = _organizationContent.Attributes;
            if (attList.GetNamedItem("identifier") != null)
            {
                _identifier = ((XmlAttribute)attList.GetNamedItem("identifier")).Value;
                _type = CourseContant.COURSE_TYPE;
            }
            XmlElement element = (XmlElement)_organizationContent;
            XmlNodeList title_list = element.GetElementsByTagName("title");
            //Get the list of title of this xmlNode and its children
            if (title_list.Count > 0)
            {
                _title = title_list[0].InnerText;//Title cua organization
            }
            if (attList.GetNamedItem("author") != null)
            {
                _author = ((XmlAttribute)attList.GetNamedItem("author")).Value;
            }
            if (attList.GetNamedItem("date") != null)
            {
                _date = ((XmlAttribute)attList.GetNamedItem("date")).Value;
            }
            if (attList.GetNamedItem("description") != null)
            {
                _description = ((XmlAttribute)attList.GetNamedItem("description")).Value;
            }
            if (attList.GetNamedItem("template") != null)
            {
                _template = ((XmlAttribute)attList.GetNamedItem("template")).Value;
            }
            XmlNodeList childList = _organizationContent.ChildNodes;
            _listItem = new List<Item>();
            for (int i = 0; i < childList.Count; i++)
            {
                //child cua organization co 2 dang : item va sequencing
                if (childList[i].Name == "item")
                {
                    Item item = new Item();
                    item.ItemContent = childList[i];
                    item.buildItem(courseTemplate);
                    _listItem.Add(item);
                }
                if (childList[i].Name == "imsss:sequencing")
                {
                    //sequencingXmlNode = childList[i];
                    _sequencing = new Sequencing();
                    _sequencing.SequencingContent = childList[i];
                    _sequencing.buildSequencing(courseTemplate);
                }
            }		
        }
        public TreeViewItem createOrganizationTreeViewItem()
        {
            TreeViewItem organizarionNode = TreeUtil.GetTreeView(_title, "CourseIcon.png");
            for (int i = 0; i < _listItem.Count; i++)
            {
                TreeViewItem treeItem = _listItem[i].createItemTreeNode();
                organizarionNode.Items.Add(treeItem);
            }
            return organizarionNode;
        }
    }
}
